package com.atguigu.spzx.manner.util;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.read.listener.ReadListener;
import com.atguigu.spzx.manner.mapper.CategoryMapper;
import com.atguigu.spzx.model.vo.CategoryExcelVo;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;

@NoArgsConstructor
public class CategoryExcelListener extends AnalysisEventListener<CategoryExcelVo> {

    private static final ArrayList<CategoryExcelVo> list = new ArrayList<>();

    private CategoryMapper categoryMapper;


    public CategoryExcelListener(CategoryMapper categoryMapper) {
        this.categoryMapper = categoryMapper;
    }

    @Override
    public void invoke(CategoryExcelVo categoryExcelVo, AnalysisContext analysisContext) {
        list.add(categoryExcelVo);
        if (list.size() >= 100) {
            categoryMapper.insertBatch(list);

            System.out.println("处理list");
            list.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

        System.out.println("处理末尾数据");
        categoryMapper.insertBatch(list);
        list.clear();


    }
}
